package com.csw.shuanfa.utils.dateTime;

import com.csw.shuanfa.utils.dateTime.HolidayEntity;
import com.csw.shuanfa.utils.dateTime.HolidayUtil2;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/testjdbc")
public class TestJdbcTemplete {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/select")
    public void testselect() {
        int count = 0;
        while (true) {
            count++;
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from user ");
            for (int i = 0; i < maps.size(); i++) {
                Map map = maps.get(i);
            }
            if (count == Math.pow(10, 6)) {
                break;
            }
            //count+"");
        }
    }

    /**
     * -- textExe.holiday definition
     * <p>
     * CREATE TABLE `holiday` (
     * `first_day` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
     * `last_day` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
     * `holiday_days` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
     *
     * @throws ParseException
     */
    @RequestMapping("/insert")
    public void testinsert() throws ParseException {

        int count = 0;
//        while (true) {
//            count++;
//            String date = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
//            jdbcTemplate.execute("insert into people values (null,'" + date + "','" + date + "')");
//
//            //count+"");
//            if (count == Math.pow(10, 6)) {
//                break;
//            }
//        }
        HolidayUtil2 holidayUtil2 = new HolidayUtil2();
        List list = holidayUtil2.judgeDate();
        try {
            for (int i = 0; i < list.size(); i++) {
                HolidayEntity holidayEntity = (HolidayEntity) list.get(i);
                String first_day = holidayEntity.getFirstDay();
                String last_day = holidayEntity.getLastDay();
                String holiday_days = String.valueOf(holidayEntity.getHolidayDays());
                List mapList = jdbcTemplate.queryForList("select * from holiday where first_day='" + first_day + "'");
                if (mapList.size() == 0) {
                    jdbcTemplate.execute("insert into holiday(first_day,last_day,holiday_days) values ('" + first_day + "','" + last_day + "','" + holiday_days + "')");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 一天一天的那种
     * http://localhost:8080/testjdbc/insertDan
     *
     * @throws ParseException * CREATE TABLE `holidaydan` (
     *                        * `day` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
     *                        * `uid` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL
     *                        * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
     */
    @RequestMapping("/insertDan")
    public void insertDan() throws ParseException {


        HolidayUtil holidayUtil = new HolidayUtil();
        List list = holidayUtil.judgeDate();
        try {
            for (int i = 0; i < list.size(); i++) {
                String holiday = (String) list.get(i);
                List mapList = jdbcTemplate.queryForList("select * from holidaydan where day='" + holiday + "'");
                if (mapList.size() == 0) {
                    jdbcTemplate.execute("insert into holidaydan(day,uid) values ('" + holiday + "','1')");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * jdbc 批量异步插入
     *
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/jdbcbatchTest")
    public void jdbcbatchTest() throws Exception {
        String sql = "insert into xxx(id,name)values(?,?)";
        //获取数据
        List<Map> dataList = new ArrayList();

        //数据格式转换
        List<Object[]> changeList = new ArrayList();
        for (int i = 0; i < dataList.size(); i++) {
            Map map = (Map) dataList.get(i);
            //数据遍历塞入相应的值
            changeList.add(new Object[]{map.get("1"), map.get("2")});
        }
        //按照500条数据分组分组
        List<List<Object[]>> blList = Lists.partition(changeList, 500);
        //每次批量执行一个组的数据，并且用异步的形式
        List<CompletableFuture> futureList = new ArrayList<>();
        for (int i = 0; i < blList.size(); i++) {
            List<Object[]> zXList = blList.get(i);
            CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {
                int[] ints = jdbcTemplate.batchUpdate(sql, zXList);
                return ints[0];
            });
            futureList.add(future);
        }

        //等待所有异步线程执行完
        for (int i = 0; i < futureList.size(); i++) {
            CompletableFuture future = futureList.get(i);
            int o = (int) future.get();
        }

    }

    /*
     *
     *
     * curl --location --request POST 'http://localhost:8080/test/jdbc/select'
     * */
}
